package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "购物车控制器")
public class ShoppingCartController {

    @Resource
    ShoppingCartService shoppingCartService;

    /**
     * 增
     *
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加商品")
    public Result<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("[添加商品到购物车] 开始添加");
        Integer tag=shoppingCartService.add(shoppingCartDTO);
        if(tag>0){
            log.info("[添加商品到购物车] 添加成功");
            return Result.success();
        }else{
            log.info("[添加商品到购物车] 添加失败");
            return Result.error("添加购物车失败");
        }
    }

    /**
     * 查
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询购物车")
    public Result<List<ShoppingCart>> findAll(){
        log.info("[查询购物车商品] 开始执行");
        List<ShoppingCart> shoppingCartList = shoppingCartService.findAll();
        log.info("[查询购物车商品] 查询成功");
        return Result.success(shoppingCartList);
    }

    /**
     * 清空
     *
     * @return
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result<String> clean(){
        log.info("[清空购物车] 开始执行");
        Integer tag = shoppingCartService.clean();
        if(tag>0){
            log.info("[清空购物车] 执行完毕");
            return Result.success();
        }else{
            log.info("[清空购物车] 清空失败");
            return Result.error("清空购物车失败");
        }
    }

    /**
     * 删单个商品
     *
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation("删除单个商品")
    @PostMapping("/sub")
    public Result<String> removeBySetmealId(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("[删除单个商品] 开始执行");
        Integer tag = shoppingCartService.removeOne(shoppingCartDTO);
        if(tag>0){
            log.info("[删除单个商品] 删除成功");
            return Result.success();
        }else{
            log.info("[删除单个商品] 删除失败");
            return Result.error("删除商品失败");
        }
    }
}